package cn._51doit.flink.day05;

import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

/**
 * 按照ProcessingTime划分回话窗口，session window按照时间的间隔生成窗口和触发的
 * 没有KeyBy，就调用windowAll方法，得到的是NonKeyedWindow，window和window operator对应的Task并行度为1
 *
 * 窗口触发的实际：当前的ProcessingTime（系统时间） -  进入到窗口中的最后一天数据对应的时间 > 指定的时间间隔
 *
 */
public class ProcessingTimeSessionWindowAllDemo {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);
        SingleOutputStreamOperator<Integer> nums = lines.map(Integer::parseInt);
        //没有keyBy，然后按照ProcessingTime划分窗口
        AllWindowedStream<Integer, TimeWindow> windowedStream = nums.windowAll(ProcessingTimeSessionWindows.withGap(Time.seconds(10)));
        //划分窗口后，需要调用相应的方法，window operator
        SingleOutputStreamOperator<Integer> res = windowedStream.sum(0);
        res.print();
        env.execute();


    }
}
